package com.tongzhan.service;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.tongzhan.base.BaseCriteria;
import com.tongzhan.base.BaseShareService;
import com.tongzhan.entity.MarkPrompt;
import com.tongzhan.entity.User;
import com.tongzhan.entity.qiaotai.OpinionCar;
import com.tongzhan.utils.DateSwitcher;
import com.tongzhan.utils.ThreadLocalUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author htmic
 * @date 2020/7/31
 */
@Service
public class OpinionCarService extends BaseShareService {

    /**
     * 列表
     * @param page
     * @param size
     * @return
     */
    public Page<OpinionCar> getList(Integer page, Integer size,String year_and_month) {
        Criteria criteria = BaseCriteria.where("is_del").is(0);
        if (!StringUtil.isEmpty(year_and_month)){
            //处理时间查询
            Date startDate = null;
            Date endDate = null;
            //查询年月 YYYY-MM
            Date date = DateSwitcher.string2Date(year_and_month, "yyyy-MM");
            startDate = DateSwitcher.getMonthFirstDay(date);
            endDate = DateSwitcher.getMonthLastDay(date);
            criteria.and("time").gte(startDate).lte(endDate);
        }
        Query query = new Query(criteria);
        PageRequest of = PageRequest.of(page, size, Sort.Direction.DESC, "addTime");
        if (ThreadLocalUtil.getThreadLocal().getDutiesCode().equals(ThreadLocalUtil.STREET_CODE)) {
            query.addCriteria(BaseCriteria.where("streetCode").like(ThreadLocalUtil.getThreadLocal().getStreetCode()));
        }
        if (ThreadLocalUtil.getThreadLocal().getDutiesCode().equals(ThreadLocalUtil.VILLAGE_CODE)) {
            query.addCriteria(BaseCriteria.where("villageCode").like(ThreadLocalUtil.getThreadLocal().getVillageCode()));
        }
        Page<OpinionCar> all = opinionCarRepository.findPage(query, of);
        return all;
    }

    /**
     * 新增侨资企业
     * @param entity
     * @return
     */
    public long add(OpinionCar entity) {
        entity.setIs_del(0);
        entity.setTime(new Date());
        entity.setAddTime(new Date());
        User userById = findUserById(entity.getAddUserId());
        if(userById!=null){
            entity.setAddUserName(userById.getUserName());
        }
        entity.setStreetCode(ThreadLocalUtil.getThreadLocal().getStreetCode());
        entity.setVillageCode(ThreadLocalUtil.getThreadLocal().getVillageCode());
        long l = opinionCarRepository.insertSelective(entity);
        if(l>0){
            //添加角标
            markPromptService.add(new MarkPrompt(2, entity.getId()));
            return  1;
        }
        return 0;
    }

    /**
     * 获取详情
     * @param id
     * @return
     */
    public OpinionCar getDetail(String id,String userId) {
        OpinionCar vo = opinionCarRepository.findByPrimaryKey(id);
        //标记已读
        markPromptService.read(id,userId);
        return vo;
    }
}
